package com.feifan.web.controller.oa;

import com.feifan.common.annotation.Log;
import com.feifan.common.core.controller.BaseController;
import com.feifan.common.core.domain.AjaxResult;
import com.feifan.common.core.page.TableDataInfo;
import com.feifan.common.enums.BusinessType;
import com.feifan.common.utils.DateUtils;
import com.feifan.common.utils.StringUtils;
import com.feifan.common.utils.poi.ExcelUtil;
import com.feifan.oa.domain.HolidayDate;
import com.feifan.oa.domain.HolidayWorkingDate;
import com.feifan.oa.service.IHolidayDateService;
import com.feifan.oa.service.IHolidayService;
import com.feifan.oa.service.IHolidayWorkingDateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * 假期时间Controller
 * @author 非凡
 * @date 2020-07-01
 */
@Controller
@RequestMapping("/oa/date")
public class HolidayDateController extends BaseController {
    private String prefix = "oa/vacation/date";

    @Autowired
    private IHolidayWorkingDateService holidayWorkingDateService;

    @Autowired
    private IHolidayDateService holidayDateService;

    @Autowired
    private IHolidayService holidayService;


    @RequiresPermissions("oa:date:view")
    @GetMapping()
    public String date(ModelMap mmap) {
        mmap.put("holidayList", holidayService.selectHolidayList(null));
        return prefix + "/date";
    }

    /**
     * 查询假期时间列表
     */
    @RequiresPermissions("oa:date:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HolidayDate holidayDate) {
        startPage();
        List<HolidayDate> list = holidayDateService.selectHolidayDateList(holidayDate);
        return getDataTable(list);
    }

    /**
     * 导出假期时间列表
     */
    @RequiresPermissions("oa:date:export")
    @Log(title = "假期时间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HolidayDate holidayDate) {
        List<HolidayDate> list = holidayDateService.selectHolidayDateList(holidayDate);
        ExcelUtil<HolidayDate> util = new ExcelUtil<HolidayDate>(HolidayDate.class);
        return util.exportExcel(list, "date");
    }

    /**
     * 新增假期时间
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("holidayList", holidayService.selectHolidayList(null));
        return prefix + "/add";
    }


    /**
     * 新增保存假期时间
     */
    @RequiresPermissions("oa:date:add")
    @Log(title = "假期时间", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HolidayDate holidayDate, String holidayWorkingDate) throws ParseException {


        //判断是否已添加当前假期的时间
        HolidayDate holidayDate1 = holidayDateService.selectHolidayDateByHId(holidayDate.getHoliday().getId());

        if (holidayDate1 != null) {
            //获取当前年份
            Calendar calendar = Calendar.getInstance();
            int a = calendar.get(Calendar.YEAR);

            //获取已添加的法定节假日年份
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(holidayDate.getBeginDate());
            System.out.println("a = " + a);
            System.out.println(calendar1.get(Calendar.YEAR));
            //判断开始时间是否为今年
            if (calendar1.get(Calendar.YEAR) == a) {
                return error("您今年已添加过当前节日!");
            }
        }

        if (!StringUtils.isEmpty(holidayWorkingDate)) {
            HolidayWorkingDate holidayWorking = new HolidayWorkingDate();
            //日期名称赋值
            holidayWorking.setHoliday(holidayDate.getHoliday());

            //获取调休日期进行分割
            String[] holidays = holidayWorkingDate.split(",");

            //循环添加调休时间
            for (String holiday : holidays) {
                holidayWorking.setDate(DateUtils.parseDate(holiday));
                holidayWorkingDateService.insertHolidayWorkingDate(holidayWorking);
            }
        }

        return toAjax(holidayDateService.insertHolidayDate(holidayDate));
    }


    /**
     * 修改假期时间
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("holidayList", holidayService.selectHolidayList(null));
        mmap.put("holidayDate", holidayDateService.selectHolidayDateById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存假期时间
     */
    @RequiresPermissions("oa:date:edit")
    @Log(title = "假期时间", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HolidayDate holidayDate, String holidayWorkingDate) throws ParseException {
        //时间格式化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //删除所有调休时间
        int r = holidayWorkingDateService.deleteHolidayWorkingDateByHId(holidayDate.getHoliday().getId());
        System.out.println("#####删除行数r = " + r);

        if (!StringUtils.isEmpty(holidayWorkingDate)) {
            HolidayWorkingDate holidayWorking = new HolidayWorkingDate();
            //日期名称赋值
            holidayWorking.setHoliday(holidayDate.getHoliday());

            //获取调休日期进行分割
            String[] holidays = holidayWorkingDate.split(",");

            //循环添加调休时间
            for (String holiday : holidays) {
                System.out.println("#####添加调休日期的天数holiday = " + holiday);
                holidayWorking.setDate(simpleDateFormat.parse(holiday));
                holidayWorkingDateService.insertHolidayWorkingDate(holidayWorking);
            }
        }
        return toAjax(holidayDateService.updateHolidayDate(holidayDate));
    }

    /**
     * 删除假期时间
     */
    @RequiresPermissions("oa:date:remove")
    @Log(title = "假期时间", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(holidayDateService.deleteHolidayDateByIds(ids));
    }
}
